package com.csair.seam.controller.pc;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csair.seam.domain.model.entity.FaMatchDetail;
import com.csair.seam.domain.model.entity.FaMatchRatio;
import com.csair.seam.domain.model.enums.VisitsModule;
import com.csair.seam.domain.model.param.FaMatchParam;
import com.csair.seam.domain.service.IFaMatchService;
import com.csair.seam.infrastructure.log.LogOperation;
import com.csair.seam.infrastructure.log.VisitsLog;
import com.csair.seam.infrastructure.poi.utils.ExcelPoiUtil;
import com.csair.seam.infrastructure.web.BaseController;
import com.csair.seam.infrastructure.web.BaseResponse;
import com.csair.seam.infrastructure.web.ResponseCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @description 效能统计分析Controller
 * @author lanrunli
 *
 */
@Api(tags = "转固设备统计分析")
@RestController
@RequestMapping("/pc/fa")
public class FaReportController extends BaseController {

    @Autowired
    private IFaMatchService faMatchService;
    
    
    @GetMapping("/match/pie")
    @ApiOperation(value = "账实相符率综合分析报表 -饼状图", notes = "getPieChartData",httpMethod="GET")
    @VisitsLog(module = VisitsModule.FA_MATCH_ANALYSIS)
    public BaseResponse<List<FaMatchRatio>> getPieChartData() {
    	List<FaMatchRatio> list = new ArrayList<>();
    	
    	FaMatchRatio allPieChartData = faMatchService.findPieChartData("all");
    	allPieChartData.setName("整体");
    	list.add(allPieChartData);
    	
    	FaMatchRatio orgPieChartData = faMatchService.findPieChartData("org");
    	orgPieChartData.setName("资产组织");
    	list.add(orgPieChartData);
    	
    	FaMatchRatio deptPieChartData = faMatchService.findPieChartData("dept");
    	deptPieChartData.setName("使用部门");
    	list.add(deptPieChartData);
    	
    	FaMatchRatio statusPieChartData = faMatchService.findPieChartData("status");
    	statusPieChartData.setName("设备状态");
    	list.add(statusPieChartData);
    	
    	return buildSuccess(list);
    }
    
    @GetMapping("/match/histogram")
    @ApiOperation(value = "账实相符率综合分析报表 -柱状图", notes = "getFaEquipRatio",httpMethod="GET")
    public BaseResponse<List<FaMatchRatio>> getHistogramData(
    		@RequestParam(defaultValue = "1") Integer groupBy,
    		@RequestParam(required = false )String orgId) {
    	if(groupBy == 2 && StringUtils.isBlank(orgId)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "请选择资产组织",null);
    	}
    	if(groupBy == 2) {//按部门
    		return buildSuccess(faMatchService.findHistogramByDept(orgId));
    	}else if(groupBy == 3) {//按使用状态
    		return buildSuccess(faMatchService.findHistogramByStatus());
    	}else {//按资产组织
    		return buildSuccess(faMatchService.findHistogramByOrg());
    	}
    	
    }
    
    @GetMapping("/match/detail")
    @ApiOperation(value = "账实相符率综合分析报表-明细 ", notes = "getFaMatchDetailPage",httpMethod="GET")
    @LogOperation(bizName = "账实相符率综合分析报表明细")
    @VisitsLog(module = VisitsModule.FA_MATCH_ANALYSIS_QUERY)
    public BaseResponse<IPage<FaMatchDetail>> getFaMatchDetailPage(@ApiParam(value = "查询参数")FaMatchParam param) {
    	return buildSuccess(faMatchService.findFaMatchDetailPage(param));
    }
    
    @GetMapping("/match/export")
    @ApiOperation(value = "账实相符率综合分析报表明细-导出 ", notes = "exportFaMatchDetail",httpMethod="GET")
    @LogOperation(bizName = "账实相符率综合分析报表表明细导出")
    @VisitsLog(module = VisitsModule.FA_MATCH_ANALYSIS_EXPORT)
    public BaseResponse<Object> exportFaMatchDetail(@ApiParam(value = "查询参数")FaMatchParam param,
            HttpServletRequest request,HttpServletResponse response) {
    	List<FaMatchDetail> data = faMatchService.findFaMatchDetailList(param);
    	if(CollectionUtils.isEmpty(data)) {
    		return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空",null);
    	}
    	ExcelPoiUtil.exportExcel("账实相符率综合分析报表明细.xlsx", data, null, request, response);
    	return buildSuccess(null);
    }
    
}

